package situ.autoweter.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import situ.autoweter.entity.RdVolume;
import situ.autoweter.entity.SyArea;
import situ.autoweter.service.RdVolumeService;
import situ.autoweter.service.SyAreaService;
import situ.autoweter.service.UsUserService;
import situ.autoweter.entity.UsUser;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
/**
 * UsUser的路由接口服务
 * 
 * @author 胡文
 *
 */
@Controller
public class UsUserController {

	/** UsUserService服务 */
	@Autowired
	private UsUserService usUserService;




	//hhl写的查询UsUser数据的方法
	@RequestMapping(value = "pay_reportBalance", produces = {"application/json;charset=UTF-8"})
	public String FindHhl(@RequestParam(defaultValue ="1",required = false) int startPage ,
						  @RequestParam(defaultValue = "300",required = false) int PageSize,UsUser value,Map map) {
		PageHelper.startPage(startPage, PageSize);
		List<UsUser> list= usUserService.find(value);
		UsUser us=usUserService.UserMoneys();
		PageInfo pageInfo = new PageInfo<>(list);
		map.put("pageInfo", pageInfo);
		map.put("startPage", startPage);
		map.put("PageSize", PageSize);

		map.put("items",list);
		map.put("itemsUS", us);
		map.put("hushu", list.size());
		return "page/pay_reportBalance";
	}
	//hhl写的预收金额查询
	@RequestMapping(value = "moneys", produces = {"application/json;charset=UTF-8"})
	public String QueryMoney(@RequestParam(defaultValue ="1",required = false) int startPage ,
							 @RequestParam(defaultValue = "2",required = false) int PageSize,Map map,@RequestParam("number") Integer number) {
		PageHelper.startPage(startPage, PageSize);
		List<Map> list= usUserService.QueryByMoney(number);
		UsUser us=usUserService.UserMoneys();
		PageInfo pageInfo = new PageInfo<>(list);
		map.put("pageInfo", pageInfo);
		map.put("startPage", startPage);
		map.put("PageSize", PageSize);

		map.put("items",list);
		map.put("itemsUS", us);
		map.put("hushu", list.size());
		map.put("num", number);
		return "page/pay_reportBalance";
	}

	/**
	 * 查询所有UsUser数据的方法
	 * @param value
	 * @return
	 */
	@RequestMapping(value = "queryUsUser", produces = {"application/json;charset=UTF-8"})
	public String find(UsUser value,Map map) {
		List<UsUser> list = usUserService.find(value);
		map.put("items", list);
		return "页面名称自己改";

	}

	@Autowired
	private SyAreaService areaService;
	@Autowired
	private RdVolumeService rdVolumeService;

	@RequestMapping(value = "rd_volume.html",produces = {"application/json;charset=UTF-8"})
	public String find(
			Map map,
			HttpServletRequest request ) {
		Integer currentPage=0;


        try {
			 currentPage = Integer.valueOf(request.getParameter("currentPage"));
			currentPage=(currentPage-1)*5;


		}catch (Exception e){
			currentPage=0;
		}





		List<Map> list=usUserService.query(currentPage,5,0);
		map.put("list", list);
		request.setAttribute("pageCount",usUserService.pageCount(0));
       List<SyArea> areas = areaService.find_onetomany();
       map.put("areas", areas);
		return "page/rd_volume.html";


 	}

	/**
	 * 通过id查询UsUser数据的方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "queryUsUserById/{id}", produces = {"application/json;charset=UTF-8"})
	public String findOne(@PathVariable(name="id") String id,Map map) {
        UsUser obj=  usUserService.findOne(id);
        map.put("UsUser",obj);
        return "页面名称自己改";
	}
	
	/**
	 * 插入UsUser属性不为空的数据方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "saveUsUser", produces = {"application/json;charset=UTF-8"})
	public String save(UsUser value) {
		  usUserService.saveNotNull(value);
        return "redirect:/queryUsUser";
	}
	
	/**
	 * 更新UsUser属性不为空的数据方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "updateUsUser", produces = {"application/json;charset=UTF-8"})
	public String update(UsUser value) {
		  usUserService.updateNotNullById(value);
          return "redirect:/queryUsUser";
	}
	
	/**
	 * 通过id删除UsUser数据方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "delVolumeById/{id}")
	public String delete(@PathVariable(name="id") Integer id) {
		 rdVolumeService.deleteById(id);
		 rdVolumeService.update1(id);
          return "redirect:/rd_volume.html";
	}


	@RequestMapping(value = "queryById/{id}")

       public String Adressquery(
			Map map,
			HttpServletRequest request,
			@PathVariable(name = "id") Integer volumeid
	){
		Integer currentPage=0;
		try {
			currentPage = Integer.valueOf(request.getParameter("currentPage"));
			currentPage=(currentPage-1)*5;


		}catch (Exception e){
			currentPage=0;
		}
		List<Map> list=usUserService.query(currentPage,5,volumeid);
		map.put("list", list);

		for (Map s: list){

			System.out.println(s);

		}


		request.setAttribute("pageCount",usUserService.pageCount(volumeid));
		List<SyArea> areas = areaService.find_onetomany();




		map.put("areas", areas);



		return  "page/rd_volume_1.html";
	}


        @RequestMapping(value = "rd_volume_add.html")
	public String addVolume1(Map map){
    List<SyArea>areas= rdVolumeService.areas();
     map.put("areas", areas);
		return "page/rd_volume_add.html";
	}
        @RequestMapping(value = "addvolume")
	 public String addVolume2(RdVolume volume

	 ){
         rdVolumeService.saveNotNull(volume);
		 return "redirect:/rd_volume.html";
	 }
         @RequestMapping(value = "updateVid")
       public String updateVid(@RequestParam("cbox")String[] cboxs,
							   @RequestParam("hi")Integer hi){

		for (String i:cboxs){
			if(rdVolumeService.queryIsNull(hi).size()!=0) {
				rdVolumeService.updateVid1(hi, i);
			}else{
				rdVolumeService.updateVid2(hi, i);
			}
		}
         return "redirect:/rd_volume.html";
	   }
       @RequestMapping(value = "updateShow/{id}")
	   public String updateShow(
	   		Map map,
			@PathVariable(name = "id")Integer ID,
			RdVolume rdVolume){
		   rdVolume=rdVolumeService.findOne(ID);
		   List<SyArea>areas= rdVolumeService.areas();
		   map.put("areas", areas);
		   map.put("rdvolume", rdVolume);

        return "page/rd_volume_update.html";
	   }

	@RequestMapping(value = "update")
	public String update(
			RdVolume rdVolume
	){
		   System.out.println(rdVolume.getID());
                 rdVolumeService.updateNotNullById(rdVolume);
                 return "redirect:/rd_volume.html";
	}



}
